/**
 * com.future CO.,ltd.
 */

package com.future.springcloud.controller;

import com.future.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * 类说明：
 */
@RestController
@RequestMapping(value = "/consumer/hystrix")
public class OrderHystrixController {

	@Resource
	private PaymentHystrixService hystrixService;

	@GetMapping(value = "/ok/{id}")
	@ResponseBody
	public String paymentInfoOK(@PathVariable("id") Integer id) {
		return hystrixService.paymentInfoOK(id);
	}

	/**
	 * 异常场景
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/timeout/{id}")
	@ResponseBody
	@HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
		@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
	})
	public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
		return hystrixService.paymentInfoTimeOut(id);
	}

	private String timeoutHandler(Integer id){
		String errMsg = String.format(Locale.ENGLISH,"系统繁忙或者运行报错，请稍后再试，id：%s",id);
		System.out.println(errMsg);
		return errMsg;
	}
}
